package com.yx.changdao.init;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.changdao.common.entity.SysMenu;
import com.yx.changdao.common.entity.SysPermission;
import com.yx.changdao.common.utils.Faster;

import com.yx.changdao.common.vo.menu.ChildrenVO;
import com.yx.changdao.common.vo.menu.MenuGlobalVo;
import com.yx.changdao.common.vo.menu.MenuVO;
import com.yx.changdao.service.SysMenuService;
import com.yx.changdao.service.SysPermissionService;
import org.nutz.json.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: <br />
 * 初始化系统菜单
 * <p>
 * <br />
 * @Auther: Zhangjiashun
 * @create 2019/11/22 16:22
 */
@Component
public class InitSysMenu {
    private static final Logger logger = LoggerFactory.getLogger(InitSysMenu.class);

    private final SysMenuService sysMenuService;
    private final SysPermissionService sysPermissionService;


    public static final String SYS_MENU = "sys:menu:manager";

    public InitSysMenu(SysMenuService sysMenuService, SysPermissionService sysPermissionService) {
        this.sysMenuService = sysMenuService;
        this.sysPermissionService = sysPermissionService;
    }

    public void init() throws Exception {
        SysPermission permission = this.sysPermissionService.getPermission(SYS_MENU);

        if (null == permission) {
            this.sysPermissionService.addPermission(SYS_MENU, "系统菜单:管理");
        }
        List<SysMenu> list = this.sysMenuService.list();
        if (CollectionUtils.isEmpty(list)) {
            logger.info("数据库中无菜单信息，初始化 菜单。。。");

            SysMenu menu = this.sysMenuService.addMenu("el-icon-menu", "系统管理", "0", "systemMg", 99, SYS_MENU);
            this.sysMenuService.addMenu("el-icon-user", "用户管理", menu.getMenuId(), "userMg", 1, "sys:user:list");
            this.sysMenuService.addMenu("el-icon-female", "角色管理", menu.getMenuId(), "roleMg", 2, "sys:role:list");
            this.sysMenuService.addMenu("el-icon-guide", "权限管理", menu.getMenuId(), "authorityMg", 3, "sys:permission:list");
            this.sysMenuService.addMenu("el-icon-tickets", "菜单管理", menu.getMenuId(), "menuMg", 4, "sys:menu:list");
        }
    }

    public Map<String, String> map = new HashMap<>();

    public void presInit() {

        map.put("首页(执行)", "menu:home:execute");
        map.put("首页(执行):首页-执行", "problem:home:execute");

        map.put("首页(总控)", "menu:home:leader");
        map.put("首页(总控):首页-总控", "problem:home:leader");

        map.put("新闻管理", "menu:news");
        map.put("新闻管理:新闻管理", "article:list");

        map.put("意见采集", "menu:problem:input");
        map.put("意见采集:录入上报", "problem:input:list");
        map.put("意见办理:上报审核", "problem:input:report:list");

        map.put("意见采集:数据查看", "problem:count:input");

        map.put("意见审批", "menu:problem:verify");
        map.put("意见审批:上报审批", "problem:report:list");
        map.put("意见审批:疑难件审批", "problem:apply:hard:list");

        map.put("意见分配", "menu:problem:assign");
        map.put("意见分配:意见分配", "problem:assign:list");

        map.put("意见办理", "menu:problem:process");
        map.put("意见办理:意见办理", "problem:process:list");
        map.put("意见办理:数据查看", "problem:count:execute");
        map.put("意见办理:难度上报", "problem:process:level");

        map.put("结果评价", "menu:problem:score");
        map.put("结果评价:回访打分", "problem:score:list");
        map.put("结果评价:重新打分", "problem:score:modify:list");
        map.put("结果评价:数据查看", "problem:count:leader");

        map.put("房屋管理(总控)", "menu:community:leader");
        //map.put("基础数据:网格管理","grid:info:list");
        map.put("房屋管理(总控):房屋管理-总控", "community:township:list");
        // map.put("基础数据:居民管理","community:resident:list");

        map.put("房屋管理(执行)", "menu:community:execute");
        map.put("房屋管理(执行):房屋管理-执行", "community:execute:list");

        map.put("权限/组织", "menu:auth:org");
        map.put("权限/组织:权限设置", "permission:set:list");
        map.put("权限/组织:账号管理", "sys:user:list");

        map.put("逾期管理", "menu:overdue");
        map.put("逾期管理:逾期管理", "problem:overdue:list");

        map.put("联户组管理(执行)", "menu:contact:execute");
        map.put("联户组管理(执行):联户组管理-执行", "dep:contact:group:list");

        map.put("联户组管理(总控)", "menu:contact:leader");
        map.put("联户组管理(总控):联户组管理-总控", "dep:contact:group:zkbm:list");

        map.put("交办单/办结单", "menu:word");
        map.put("交办单/办结单:交办单/办结单", "word:export:list");

        map.put("交办单", "menu:execute:word");
        map.put("交办单:交办单-执行", "execute:word:export:list");

        map.put("系统设置", "menu:seting");
        map.put("系统设置:系统设置", "no:workday:list");
    }


    public boolean saveOrUpdateByJSON(String json) {
        /**
         * 初始化
         */
        presInit();

        MenuGlobalVo glo = Json.fromJson(MenuGlobalVo.class, json);

        if (Faster.isNull(glo)) {
            return false;
        }

        List<MenuVO> data = glo.getData();

        int i = 0;

        for (MenuVO t : data) {

            SysMenu father = sysMenuService.getOne(new QueryWrapper<SysMenu>()
                    .eq(SysMenu.MENU_NAME, t.getMeta().getTitle())
                    .eq(SysMenu.MENU_FILE, t.getName())
                    .eq(SysMenu.MENU_URI, t.getPath()));

            if (Faster.isNull(father)) {

                SysMenu sysMenu = new SysMenu();
                sysMenu.setMenuUri(t.getPath());
                sysMenu.setMenuFile(t.getName());
                sysMenu.setMenuName(t.getMeta().getTitle());
                sysMenu.setMenuIcon(t.getMeta().getIcon());
                sysMenu.setMenuParent("0");

                sysMenu.setMenuSort(i++);

//对于重名的菜单进行单独的逻辑判断/////////////////////////////////////////////////////////////////////////////

                if ("/home-execute".equals(t.getRedirect())) { //执行首页
                    String mName = map.get(sysMenu.getMenuName() + "(执行)");
                    sysMenu.setPermissionId(mName);

                } else if ("/home-control".equals(t.getRedirect())) { //总控首页
                    String mName = map.get(sysMenu.getMenuName() + "(总控)");
                    sysMenu.setPermissionId(mName);

                } else if (sysMenu.getMenuUri().equals("/controlBuildDetailContact")) { //总控联户组

                    String mName = map.get(sysMenu.getMenuName() + "(总控)");
                    sysMenu.setPermissionId(mName);

                } else if (sysMenu.getMenuUri().equals("/buildDetailContact")) { //执行联户组

                    String mName = map.get(sysMenu.getMenuName() + "(执行)");
                    sysMenu.setPermissionId(mName);
                } else if (sysMenu.getMenuUri().equals("/buildmanagement")) { //总控房屋

                    String mName = map.get(sysMenu.getMenuName() + "(总控)");
                    sysMenu.setPermissionId(mName);

                } else if (sysMenu.getMenuUri().equals("/buildmanagement1")) { //执行房屋

                    String mName = map.get(sysMenu.getMenuName() + "(执行)");
                    sysMenu.setPermissionId(mName);
                } else {
                    sysMenu.setPermissionId(map.get(sysMenu.getMenuName()));
                }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                sysMenuService.save(sysMenu);

                //如果父级菜单不存在,假定字节菜单也不存在
                List<ChildrenVO> children = t.getChildren();

                for (ChildrenVO c : children) {

                    SysMenu chi = new SysMenu();
                    chi.setMenuUri(c.getPath());
                    chi.setMenuFile(c.getName());
                    chi.setMenuName(c.getMeta().getTitle());

                    chi.setMenuParent(sysMenu.getMenuId()); //父类ID

                    chi.setMenuSort(i++);

//对于重名的菜单进行单独的逻辑判断/////////////////////////////////////////////////////////////////////////////
                    if (chi.getMenuUri().equals("home-execute")) { //执行首页
                        String mName = map.get(sysMenu.getMenuName() + "(执行)" + ":" + chi.getMenuName());
                        chi.setPermissionId(mName);

                    } else if (chi.getMenuUri().equals("home-control")) { //总控首页
                        String mName = map.get(sysMenu.getMenuName() + "(总控)" + ":" + chi.getMenuName());
                        chi.setPermissionId(mName);
                    } else if (chi.getMenuUri().equals("controlBuildDetailContact")) { //总控联户组
                        String mName = map.get(sysMenu.getMenuName() + "(总控)" + ":" + chi.getMenuName());
                        chi.setPermissionId(mName);

                    } else if (chi.getMenuUri().equals("buildDetailContact")) { //执行联户组
                        String mName = map.get(sysMenu.getMenuName() + "(执行)" + ":" + chi.getMenuName());
                        chi.setPermissionId(mName);
                    } else if (chi.getMenuUri().equals("buildmanagement")) { //总控房屋
                        String mName = map.get(sysMenu.getMenuName() + "(总控)" + ":" + chi.getMenuName());
                        chi.setPermissionId(mName);
                    } else if (chi.getMenuUri().equals("buildmanagement1")) { //执行房屋
                        String mName = map.get(sysMenu.getMenuName() + "(执行)" + ":" + chi.getMenuName());
                        chi.setPermissionId(mName);

                    } else {
                        chi.setPermissionId(map.get(sysMenu.getMenuName() + ":" + chi.getMenuName()));
                    }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    sysMenuService.save(chi);
                }
            }
        }
        return true;
    }

}
